/**** UNSC and involvement in MIDS ***/ 
/* Lastest Version November 3rd, 2022. Alastair Smith */
/* This program file (written for Stata 17) executes the analyses contained in "UN Security Council Membership: Increased Security and Reduced Conflict" -- Alastair Smith and James Vreeland. 
*/

#delimit;
/* Location of the directory with the data */
global datadir "~Dropbox/2021/temp/dataverse_files (4)"; /* change to the directory where data is*/

/* Subdirectory to store results, graphs and tables */
global outty ""; /* specific where you want the output stored */

capture cd $datadir; 
/* log the results */ 
capture log close;
log using "outputUNSCdeterrence_Nov2022.smcl", replace;

/************ Data Organization ****************/ 
/* There are three main data sets: 
1) Monadic Data
2) Dyadic data
3) Concessions with terroitorial disputes
The constitutent data are organized to create the three datasets for the analysis in  OrganizeDataUNSCdeter.do 
 
do OrganizeDataUNSCdeter.do; */


/*****************************************************************/
/*****************************************************************/
/******************* Monadic Analyses       **********************/
/*****************************************************************/
/*****************************************************************/
/*****************************************************************/
#delimit ;

use monadic_working,clear; 
keep  if year<=2014 &perm== 0 & year>=1951 ;
sort ccode year nobs; 
by ccode year: gen nn =_n;

 replace SIDEA=0 if SIDEA==.;
replace SIDEB=0 if SIDEB==.;
replace MID=0 if MID==.;
replace mid=0 if mid==.;
replace orig=0 if orig==.;
replace sidea=0 if sidea==. ;
replace sideb =0 if sideb==.;
gen demaut=(10+polity)/20;
gen yr=(year-1990)/10;
gen yr2=yr^2;
gen yr3=yr^3;
by ccode year: egen sumSIDEA=sum(SIDEA);
by ccode year: egen sumSIDEB=sum(SIDEB);
by ccode year: egen sumMID=sum(MID);
save temp, replace;
sort ccode year nn; 

keep if nn==1;
xtset ccode year;
gen LMID=L.MID;
gen L2MID=L2.MID;
gen Lmid=L.mid;
gen L2mid=L2.mid;
gen LSIDEA=L.SIDEA;
gen L2SIDEA=L2.SIDEA;
gen LSIDEB=L.SIDEB;
gen L2SIDEB=L2.SIDEB;

gen LHostA=L.HostA;
gen L2HostA=L2.HostA;
gen LHostB=L.HostB;
gen L2HostB=L2.HostB;
gen Lwar=L.war; gen L2war=L2.war; 
gen Lcwar=L.cwar; gen L2cwar=L2.cwar;
gen LcwarS=L.cwarS; gen L2cwarS=L2.cwarS;
gen LcwarIS=L.cwarIS; gen L2cwarIS=L2.cwarIS;
gen LcwarI=L.cwarI; gen L2cwarI=L2.cwarI;

keep ccode year L* ;
merge 1:m ccode year using  temp;
gen power =100*cinc;
gen pre =(tm1==1|tm2==1);
gen post =(t3==1|t4==1);
sort ccode year nn;
by ccode year : gen single =(_n==1); 

#delimit;
global lab "unsc "UNSC"  power "Power" demaut "Polity" pre "Prior to UNSC" 
post  "Post UNSC" t0 "Year of Election" SIDEA "Initiator" SIDEB "Target" 
growthWB "Growth" lgdppc "Log(GDPpc)" lpop "Log(Population)" aidGNI  "Aid/GNI"
ResRents "Resource Rents/GDP" MID "Dispute\~_{t}\~ " LMID "Dispute\~_{t-1}\~ " 
L2MID  "Dispute\~_{t-2}\~ " SIDEA "Initiator\~_{t}\~ " 
LSIDEA "Initiator\~_{t-1}\~ " L2SIDEA "Initiator\~_{t-2}\~ " SIDEB "Target\~_{t}\~ " 
LSIDEB "Target\~_{t-1}\~ " L2SIDEB "Target\~_{t-2}\~ " demaut "Polity" cwar "Civil War\~_{t}\~"  war "War\~_{t}\~" Lcwar "Civil War\~_{t-1}\~  " Lwar "War\~_{t-1}\~  "  rotate "Normalized Time Since UNSC" myturn "Turn Due" 
SCratio "Seats/Valid Candidates" odd "Odd Year" logpopVDEM log(Population)  e_migdppcln "log(GDPpc)" PrUNSCpooled "Pr(Elected UNSC)" PrUNSCregions "Pr(Elected UNSC\~_{regions}\~)" 
FPrUNSCpooled "Pr(Elected UNSC\~_{t+1}\~)"
FPrUNSCregions "Pr(Elected UNSC\~_{t+1,regions}\~)" 
PrUNSCany "Pr(Elected UNSC)"
FPrUNSCany "Pr(Elected UNSC\~_{t+1}\~)"
unscwealth "UNSC*log(GDPpc)"
PrUNSCpooledwealth "Pr(Elected UNSC)*log(GDPpc)" FPrUNSCpooledwealth "Pr(Elected UNSC\~_{t+1}\~)*log(GDPpc)" 
PrUNSCanywealth "Pr(Elected UNSC)*log(GDPpc)" FPrUNSCanywealth "Pr(Elected UNSC\~_{t+1}\~)*log(GDPpc)" 
PrUNSCpoorauto "Pr(Elected UNSC)*Poor "  FPrUNSCpoorauto "Pr(Elected UNSC\~_{t+1}\~)*Poor"
poorauto "Poor, Non-democracy" 
poorautoA "Poor, non-democratic A" poorautoB "Poor, non-democratic B"  P5Ally "P5 Ally"    ";


label define un -1 "Prior (t-1,t-2)" 0 "Election Year" -2 "No UNSC" 1 "UNSC" 2 "Post UNSC (t+1,t+2)" ,replace; 


#delimit;
gen un = -2;  replace un=-1 if tm2==1| tm1==1 ; replace un =0 if t0==1;
replace un=1 if t2==1| t1==1; replace un=2 if t3==1| t4==1; 
label values un un;


#delimit;
/*****************************************************************/
/*****************************************************************/
/****** Analyses of election to the UNCS**************************/
/*****************************************************************/
/*****************************************************************/

 /****** Election to UNSC: t0=1 in the year a nation is elected *******/ 
   logit t0 (c.rotate c.myturn  c.SCratio   c.demaut  c.logpopVDEM   c.e_migdppcln cwar Lcwar     MID LMID   ) if notElection==0&single==1 ;
   eststo m1; estadd local fe "N";
    xtlogit t0 (c.rotate c.myturn  c.SCratio   c.demaut  c.logpopVDEM   c.e_migdppcln cwar Lcwar     MID LMID   ) if notElection==0&single==1 , fe i(ccode) ;
   eststo m1fe; estadd local fe "Y";
   
   logit t0 (c.rotate c.myturn  c.SCratio   c.demaut  c.logpopVDEM   c.e_migdppcln cwar Lcwar       SIDEA LSIDEA   SIDEB LSIDEB) if notElection==0 &single==1;eststo m2; estadd local fe "N";
   xtlogit t0 (c.rotate c.myturn  c.SCratio   c.demaut  c.logpopVDEM   c.e_migdppcln cwar Lcwar       SIDEA LSIDEA   SIDEB LSIDEB) if notElection==0 &single==1, fe i(ccode) ;eststo m2fe; estadd local fe "Y";
   
   logit t0 (c.rotate c.myturn  c.SCratio   cwar Lcwar       SIDEA LSIDEA   SIDEB LSIDEB) if notElection==0 &single==1;eststo m3; estadd local fe "N";
 
 logit t0 (c.rotate c.myturn  c.SCratio   c.demaut  c.logpopVDEM   c.e_migdppcln cwar Lcwar       ) if notElection==0&single==1 ;eststo m4; estadd local fe "N";
 xtlogit t0 (c.rotate c.myturn  c.SCratio   c.demaut  c.logpopVDEM   c.e_migdppcln cwar Lcwar       ) if notElection==0&single==1 ,fe i(ccode);eststo m4fe; estadd local fe "Y";

  
global mlab " "Election" "Election" "Election"  " ;
esttab m1 m2  m4  using "${outty}RotateElection.tex",replace unstack ar2 r2 se 
   title("Election to UNSC and Prior Dispute Involvement \label{tab:RotateElection}" )
   stats( N  fe ,
	labels("Observations" "Country FE"  )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab) order(rotate myturn  SCratio MID LMID SIDEA LSIDEA  SIDEB LSIDEB cwar Lcwar  demaut  logpopVDEM   e_migdppcln ) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) 
 drop(    _cons) mlabels($mlab, nonumbers) eqlabels(none) substitute(\~ $); 
 /* table with FEs */
esttab m1 m2  m4 m1fe m2fe m4fe using "${outty}RotateElectionFE.tex",replace unstack ar2 r2 se 
   title("Election to UNSC and Prior Dispute Involvement \label{tab:RotateElectionFE}" )
   stats( N  fe ,
	labels("Observations" "Country FE"  )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab ) order(rotate myturn  SCratio MID LMID SIDEA LSIDEA  SIDEB LSIDEB cwar Lcwar  demaut  logpopVDEM   e_migdppcln ) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) 
 drop(    _cons) mlabels($mlab $mlab, nonumbers) eqlabels(none) substitute(\~ $); 
 
#delimit;
/** region by region **/

/*Africa*/ 
logit t0 (c.rotate c.myturn  c.SCratio  c.power c.demaut  c.lpop   c.lgdppc cwar Lcwar      mid Lmid  ) if notElection==0 &unsc_region==1&single==1;
lroc, nograph;
/* Asia*/ 
logit t0 (c.rotate c.myturn  c.SCratio  c.power c.demaut  c.lpop   c.lgdppc cwar Lcwar     mid Lmid  ) if notElection==0 &unsc_region==2&single==1;
lroc, nograph;
/* EEurope */     
logit t0 (c.rotate c.myturn  c.SCratio  c.power c.demaut  c.lpop   c.lgdppc cwar Lcwar     mid Lmid  ) if notElection==0 &unsc_region==3&single==1;
lroc, nograph;
/* WEOG */
logit t0 (c.rotate c.myturn  c.SCratio  c.power c.demaut  c.lpop   c.lgdppc cwar Lcwar      mid Lmid  ) if notElection==0 &unsc_region==4&single==1;
lroc, nograph;
/* Latin Am*/
logit t0 (c.rotate c.myturn  c.SCratio  c.power c.demaut  c.lpop   c.lgdppc cwar Lcwar      mid Lmid  )  if notElection==0 &unsc_region==5&single==1;
lroc, nograph ;

logit t0 (c.rotate c.myturn  c.SCratio  c.power c.demaut  c.lpop   c.lgdppc cwar Lcwar      mid Lmid  )  if notElection==0 &single==1;
lroc,nograph;
  
/* Region by region predicted probability of election without MIDS */ 
global emodel "c.rotate c.myturn  c.SCratio   c.demaut  c.logpopVDEM   c.e_migdppcln cwar Lcwar";
logit t0 ($emodel  ) if notElection==0 &single==1;
capture drop PrUNSCpooled; predict PrUNSCpooled, pr;
replace PrUNSCpooled=0 if notElection==1; 

gen PrUNSCregions=0;

logit t0 ($emodel   ) if notElection==0 & unsc_region ==1 &single==1; 
capture drop temp; predict temp if e(sample)==1, pr; 
regen PrUNSCregions=temp if e(sample)==1,replace;

logit t0 ($emodel    ) if notElection==0 & unsc_region ==2 &single==1; 
capture drop temp; predict temp if e(sample)==1, pr; 
regen PrUNSCregions=temp if e(sample)==1,replace;

logit t0 ($emodel    ) if notElection==0 & unsc_region ==3 &single==1; 
capture drop temp; predict temp if e(sample)==1, pr; 
regen PrUNSCregions=temp if e(sample)==1,replace;

logit t0 ($emodel   ) if notElection==0 & unsc_region ==4 &single==1; 
capture drop temp; predict temp if e(sample)==1, pr; 
regen PrUNSCregions=temp if e(sample)==1,replace;

logit t0 ($emodel  ) if notElection==0 & unsc_region ==5 &single==1; 
capture drop temp; predict temp if e(sample)==1, pr; 
regen PrUNSCregions=temp if e(sample)==1,replace;
drop temp;  drop _m;
keep if single==1; 
merge 1:1 ccode year using PublicChoiceProbElection ;

#delimit;
drop if _m==2; 
drop _m iso;
sort ccode year;
xtset ccode year;
 foreach i of varlist PrUNSCbig PrUNSCsmall PrUNSCnoFE PrUNSCpooled PrUNSCregions {;
	gen F`i'=F.`i';
	gen L`i'=L.`i';
};
 sort ccode year;
/* store predictions in a separate frame */
capture frame drop probUNSC; 
frame put ccode year PrUNSCregions PrUNSCpooled FPrUNSCpooled FPrUNSCregions PrUNSCbig PrUNSCsmall PrUNSCnoFE FPrUNSCbig LPrUNSCbig FPrUNSCsmall LPrUNSCsmall FPrUNSCnoFE LPrUNSCnoFE if single==1, into(probUNSC);
frame probUNSC: sort ccode year;
/* Add predicted probability of election to UNSC based on Analysis in The Determinants of Election to the United Nations Security Council},
  author={Dreher, Axel and Gould, Matthew and Rablen, Matthew D and Vreeland, James Raymond},  journal={Public Choice},  year={2014}
  Three estimates big model, small model and no fixed effect model. */ 
frame probUNSC: save probUNSC.dta, replace; 

 
   /*** MID Onset -- observed data ***/
#delimit; 
global model " pre t0 unsc post   c.power c.demaut  c.logpopVDEM   c.e_migdppcln yr* if (OBSperNatYear<2 |OBSperNatYear==.) &  unsc==0 &single==1";
xtlogit MID  $model, fe i(ccode) ; eststo m1midfe; estadd local fe "Y";estadd local ttrend "Y";
xtlogit SIDEA  $model, fe i(ccode) ;eststo m2midfe; estadd local fe "Y";estadd local ttrend "Y";
xtlogit SIDEB $model, fe i(ccode) ;eststo m3midfe; estadd local fe "Y";estadd local ttrend "Y";
/* without FE */
logit MID  $model, ;eststo m1mid; estadd local fe "N";estadd local ttrend "Y";
logit SIDEA  $model,  ;
eststo m2mid; estadd local fe "N";estadd local ttrend "Y";
logit SIDEB $model, ;
eststo m3mid; estadd local fe "N";estadd local ttrend "Y";


global mlab " "MID" "Initiator" "Target"  " ;
esttab m1mid m2mid m3mid m1midfe m2midfe m3midfe  using "${outty}ObservedUNSC.tex",replace unstack ar2 r2 se 
   title("MID involvement and membership of UNSC \label{tab:ObservedUNSC}" )
   stats( N  fe ttrend,
	labels("Observations" "Country FE" "Time Trends" )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab) order(pre t0 unsc post power demaut  logpopVDEM   e_migdppcln) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) 
 drop( yr* unsc _cons  ) mlabels( $mlab $mlab, nonumbers) eqlabels(none) substitute(\~ $); 
 
  /*** MID Onset -- pooled predicted probability of UNSC (included UNSC years ) ***/
#delimit; 
global model "unsc PrUNSCpooled c.power c.demaut  c.logpopVDEM   c.e_migdppcln P5Ally yr* if single==1";
xtlogit MID  $model, fe i(ccode) ; eststo m1midfe; estadd local fe "Y";estadd local ttrend "Y";
xtlogit SIDEA  $model, fe i(ccode) ;eststo m2midfe; estadd local fe "Y";estadd local ttrend "Y";
xtlogit SIDEB $model, fe i(ccode) ;eststo m3midfe; estadd local fe "Y";estadd local ttrend "Y";
/* without FE */
logit MID  $model, ;eststo m1mid; estadd local fe "N";estadd local ttrend "Y";
logit SIDEA  $model,  ;
eststo m2mid; estadd local fe "N";estadd local ttrend "Y";
logit SIDEB $model, ;
eststo m3mid; estadd local fe "N";estadd local ttrend "Y";

global mlab " "MID" "Initiator" "Target"  " ;
esttab m1mid m2mid m3mid m1midfe m2midfe m3midfe  using "${outty}MidPrUNSCpooledunsc.tex",replace unstack ar2 r2 se 
   title("MID involvement and the likelihood of election to UNSC \label{tab:MidPrUNSCpooled}" )
   stats( N  fe ttrend,
	labels("Observations" "Country FE" "Time Trends" )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab) order(unsc  PrUNSCpooled power demaut  logpopVDEM   e_migdppcln) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) 
 drop( yr*  _cons  ) mlabels( $mlab $mlab, nonumbers) eqlabels(none) substitute(\~ $); 
 
#delimit; 
global model "unsc    FPrUNSCpooled PrUNSCpooled c.power c.demaut  c.logpopVDEM   c.e_migdppcln P5Ally yr* if  single==1 ";
xtlogit MID  $model, fe i(ccode) ; eststo m1midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCpooled PrUNSCpooled;

test FPrUNSCpooled+ PrUNSCpooled=0;
xtlogit SIDEA  $model, fe i(ccode) ;eststo m2midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCpooled PrUNSCpooled;test FPrUNSCpooled+ PrUNSCpooled=0;
xtlogit SIDEB $model, fe i(ccode) ;eststo m3midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCpooled PrUNSCpooled;test FPrUNSCpooled+ PrUNSCpooled=0;
/* without FE */
logit MID  $model, ;eststo m1mid; estadd local fe "N";estadd local ttrend "Y";test FPrUNSCpooled PrUNSCpooled;
logit SIDEA  $model,  ;
eststo m2mid; estadd local fe "N";estadd local ttrend "Y";
logit SIDEB $model, ;test FPrUNSCpooled PrUNSCpooled;
eststo m3mid; estadd local fe "N";estadd local ttrend "Y";test FPrUNSCpooled PrUNSCpooled;

global mlab " "MID" "Initiator" "Target"  " ;
esttab m1mid m2mid m3mid m1midfe m2midfe m3midfe  using "${outty}MidPrUNSCpooledunscF.tex",replace unstack ar2 r2 se 
   title("MID involvement and the likelihood of election to UNSC (Pr(Elected to UNSC) based on model 3 of table~\ref{tab:RotateElectionFE}) \label{tab:MidPrUNSCpooledunscF}" )
   stats( N  fe ttrend,
	labels("Observations" "Country FE" "Time Trends" )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab) order(unsc   FPrUNSCpooled PrUNSCpooled power demaut  logpopVDEM   e_migdppcln) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) 
 drop( yr*  _cons  ) mlabels( $mlab $mlab, nonumbers) eqlabels(none) substitute(\~ $);  



#delimit; 
/* restrict analysis to UN members only */
global model "unsc    FPrUNSCpooled PrUNSCpooled c.power c.demaut  c.logpopVDEM   c.e_migdppcln P5Ally yr* if  single==1 & UNmember==1";
xtlogit MID  $model, fe i(ccode) ; eststo m1midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCpooled PrUNSCpooled;

test FPrUNSCpooled+ PrUNSCpooled=0;
xtlogit SIDEA  $model, fe i(ccode) ;eststo m2midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCpooled PrUNSCpooled;test FPrUNSCpooled+ PrUNSCpooled=0;
xtlogit SIDEB $model, fe i(ccode) ;eststo m3midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCpooled PrUNSCpooled;test FPrUNSCpooled+ PrUNSCpooled=0;
/* without FE */
logit MID  $model, ;eststo m1mid; estadd local fe "N";estadd local ttrend "Y";test FPrUNSCpooled PrUNSCpooled;
logit SIDEA  $model,  ;
eststo m2mid; estadd local fe "N";estadd local ttrend "Y";
logit SIDEB $model, ;test FPrUNSCpooled PrUNSCpooled;
eststo m3mid; estadd local fe "N";estadd local ttrend "Y";test FPrUNSCpooled PrUNSCpooled;

global mlab " "MID" "Initiator" "Target"  " ;
esttab m1mid m2mid m3mid m1midfe m2midfe m3midfe  using "${outty}MidPrUNSCpooledunscFunmembers.tex",replace unstack ar2 r2 se 
   title("MID involvement and the likelihood of election to UNSC (Pr(Elected to UNSC) based on model 3 of table~\ref{tab:RotateElectionFE}) \label{tab:MidPrUNSCpooledunscFunmembers}" )
   stats( N  fe ttrend,
	labels("Observations" "Country FE" "Time Trends" )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab) order(unsc   FPrUNSCpooled PrUNSCpooled power demaut  logpopVDEM   e_migdppcln) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) 
 drop( yr*  _cons  ) mlabels( $mlab $mlab, nonumbers) eqlabels(none) substitute(\~ $); 

/* Count model: Poisson */  
 #delimit; 
global model "unsc    FPrUNSCpooled PrUNSCpooled c.power c.demaut  c.logpopVDEM   c.e_migdppcln P5Ally yr* if  single==1 ";
 xtpoisson sumMID  $model, fe i(ccode) ; eststo m1midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCpooled PrUNSCpooled;
test FPrUNSCpooled+ PrUNSCpooled=0;

 xtpoisson sumSIDEA  $model, fe i(ccode) ;eststo m2midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCpooled PrUNSCpooled;test FPrUNSCpooled+ PrUNSCpooled=0;
 xtpoisson sumSIDEB $model, fe i(ccode) ;eststo m3midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCpooled PrUNSCpooled;test FPrUNSCpooled+ PrUNSCpooled=0;
/* without FE */
 poisson sumMID  $model, ;eststo m1mid; estadd local fe "N";estadd local ttrend "Y";test FPrUNSCpooled PrUNSCpooled;
poisson sumSIDEA  $model,  ;
eststo m2mid; estadd local fe "N";estadd local ttrend "Y";
poisson sumSIDEB $model, ;test FPrUNSCpooled PrUNSCpooled;
eststo m3mid; estadd local fe "N";estadd local ttrend "Y";test FPrUNSCpooled PrUNSCpooled;

global mlab " "MID" "Initiator" "Target"  " ;
esttab m1mid m2mid m3mid m1midfe m2midfe m3midfe  using "${outty}MidPrUNSCpooledunscFcount.tex",replace unstack ar2 r2 se 
   title("Count model of MID and the likelihood of election to UNSC (Pr(Elected to UNSC) based on model 3 of table~\ref{tab:RotateElectionFE}) \label{tab:MidPrUNSCpooledunscFcount}" )
   stats( N  fe  ,
	labels("Observations" "Country FE"   )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab) order(unsc   FPrUNSCpooled PrUNSCpooled power demaut  logpopVDEM   e_migdppcln) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) 
 drop( yr*  _cons  ) mlabels( $mlab $mlab, nonumbers) eqlabels(none) substitute(\~ $); 
 
 
/* Count model: nbreg */  
 #delimit; 
global model "unsc    FPrUNSCpooled PrUNSCpooled c.power c.demaut  c.logpopVDEM   c.e_migdppcln P5Ally yr* if  single==1 ";
 xtnbreg sumMID  $model, fe i(ccode) ; eststo m1midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCpooled PrUNSCpooled;
test FPrUNSCpooled+ PrUNSCpooled=0;

 xtnbreg sumSIDEA  $model, fe i(ccode) ;eststo m2midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCpooled PrUNSCpooled;test FPrUNSCpooled+ PrUNSCpooled=0;
 xtnbreg sumSIDEB $model, fe i(ccode) ;eststo m3midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCpooled PrUNSCpooled;test FPrUNSCpooled+ PrUNSCpooled=0;
/* without FE */
#delimit;
 nbreg sumMID  $model, ;eststo m1mid; estadd local fe "N";estadd local ttrend "Y";test FPrUNSCpooled PrUNSCpooled;
nbreg sumSIDEA  $model,  ;
eststo m2mid; estadd local fe "N";estadd local ttrend "Y";
nbreg sumSIDEB $model, ;test FPrUNSCpooled PrUNSCpooled;
eststo m3mid; estadd local fe "N";estadd local ttrend "Y";test FPrUNSCpooled PrUNSCpooled;

global mlab " "MID" "Initiator" "Target"  " ;
esttab m1mid m2mid m3mid m1midfe m2midfe m3midfe  using "${outty}MidPrUNSCpooledunscFnbreg.tex",replace unstack ar2 r2 se 
   title("Negative binominal model of MID and the likelihood of election to UNSC (Pr(Elected to UNSC) based on model 3 of table~\ref{tab:RotateElectionFE}) \label{tab:MidPrUNSCpooledunscFnbreg}" )
   stats( N  fe ttrend,
	labels("Observations" "Country FE" "Time Trends" )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab) order(unsc   FPrUNSCpooled PrUNSCpooled power demaut  logpopVDEM   e_migdppcln) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) 
 drop( yr*  _cons  ) mlabels( $mlab $mlab, nonumbers) eqlabels(none) substitute(\~ $); 
 
/********************** Robustness Test Looking at Election Probabilty based on pooled analysis and also using estimates from  Analysis in The Determinants of Election to the United Nations Security Council. Dreher, Axel and Gould, Matthew and Rablen, Matthew D and Vreeland, James Raymond. Public Choice 2014  *********/
#delimit; 
/* Involvement in a MID */
/*pooled */
regen PrUNSCany=PrUNSCpooled,replace; regen FPrUNSCany=FPrUNSCpooled,replace;
global model "unsc    FPrUNSCany PrUNSCany c.power c.demaut  c.logpopVDEM   c.e_migdppcln yr* if (OBSperNatYear<2 |OBSperNatYear==.)  &single==1";
logit MID  $model  ; eststo m1mid; estadd local fe "N";estadd local ttrend "Y"; test FPrUNSCany PrUNSCany; test FPrUNSCany+ PrUNSCany=0;
xtlogit MID  $model, fe i(ccode)  ; eststo m1midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCany PrUNSCany; test FPrUNSCany+ PrUNSCany=0;

/*regions */
regen PrUNSCany=PrUNSCregions,replace; regen FPrUNSCany=FPrUNSCregions,replace;
global model "unsc    FPrUNSCany PrUNSCany c.power c.demaut  c.logpopVDEM   c.e_migdppcln yr* if (OBSperNatYear<2 |OBSperNatYear==.)  &single==1";
logit MID  $model  ; eststo m2mid; estadd local fe "N";estadd local ttrend "Y"; test FPrUNSCany PrUNSCany; test FPrUNSCany+ PrUNSCany=0;
xtlogit MID  $model, fe i(ccode)  ; eststo m2midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCany PrUNSCany; test FPrUNSCany+ PrUNSCany=0;

/*big */
regen PrUNSCany=PrUNSCbig,replace; regen FPrUNSCany=FPrUNSCbig,replace;
global model "unsc    FPrUNSCany PrUNSCany c.power c.demaut  c.logpopVDEM   c.e_migdppcln yr* if (OBSperNatYear<2 |OBSperNatYear==.)  &single==1";
logit MID  $model  ; eststo m3mid; estadd local fe "N";estadd local ttrend "Y"; test FPrUNSCany PrUNSCany; test FPrUNSCany+ PrUNSCany=0;
xtlogit MID  $model, fe i(ccode)  ; eststo m3midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCany PrUNSCany; test FPrUNSCany+ PrUNSCany=0;

/*small */
regen PrUNSCany=PrUNSCsmall,replace; regen FPrUNSCany=FPrUNSCsmall,replace;
global model "unsc    FPrUNSCany PrUNSCany c.power c.demaut  c.logpopVDEM   c.e_migdppcln yr* if (OBSperNatYear<2 |OBSperNatYear==.)  &single==1";
logit MID  $model  ; eststo m4mid; estadd local fe "N";estadd local ttrend "Y"; test FPrUNSCany PrUNSCany; test FPrUNSCany+ PrUNSCany=0;
xtlogit MID  $model, fe i(ccode)  ; eststo m4midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCany PrUNSCany; test FPrUNSCany+ PrUNSCany=0;

#delimit;
global mlab " "Regions" "Big Model" "Small Model" "Regions" "Big Model" "Small Model" " ;
esttab m2mid m3mid m4mid m2midfe m3midfe m4midfe  using "${outty}MidPrUNSCrobust.tex",replace unstack ar2 r2 se 
   title("MID involvement and the likelihood of election to UNSC: Estimates of Pr(Elected) based on Region-by-Region Estimates and Large and Small Models in \cite{dreher2014determinants} \label{tab:MidPrUNSCrobust}" )
   stats( N  fe  ,
	labels("Observations" "Country FE"   )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab) order(unsc   FPrUNSCany PrUNSCany power demaut  logpopVDEM   e_migdppcln) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) 
 drop( yr*  _cons  ) mlabels( $mlab $mlab, nonumbers) eqlabels(none) substitute(\~ $);  
 

#delimit; /* INTERACT WEALTH AND PR(ELECT)
high pr of election reduces being target -- especial for non-democratic and poor -- poor nations make concessions */
regen PrUNSCany=PrUNSCpooled,replace; regen FPrUNSCany=FPrUNSCpooled,replace;
regen PrUNSCanywealth = PrUNSCany*e_migdppcln ,replace;
regen FPrUNSCanywealth = FPrUNSCany*e_migdppcln ,replace;
regen unscwealth=unsc*e_migdppcln ,replace;
global mod " c.unsc c.FPrUNSCany c.FPrUNSCanywealth  c.PrUNSCany c.PrUNSCanywealth   c.power c.demaut  c.logpopVDEM   c.e_migdppcln P5Ally yr* if  single==1 ";


xtlogit MID  $mod, fe i(ccode) ; eststo m1midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCany PrUNSCany; test FPrUNSCany+ PrUNSCany=0;
xtlogit SIDEA  $mod, fe i(ccode) ;eststo m2midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCany PrUNSCany; test FPrUNSCany+ PrUNSCany=0;
xtlogit SIDEB $mod, fe i(ccode) ;eststo m3midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCany PrUNSCany; test FPrUNSCany+ PrUNSCany=0;
/* without FE */
logit MID  $mod, ;eststo m1mid; estadd local fe "N";estadd local ttrend "Y";test FPrUNSCany PrUNSCany; test FPrUNSCany+ PrUNSCany=0;
logit SIDEA  $mod ,  ;
eststo m2mid; estadd local fe "N";estadd local ttrend "Y";
logit SIDEB $mod, ;test FPrUNSCany PrUNSCany; test FPrUNSCany+ PrUNSCany=0;
eststo m3mid; estadd local fe "N";estadd local ttrend "Y";test FPrUNSCany PrUNSCany; test FPrUNSCany+ PrUNSCany=0;

global mlab " "MID" "Initiator" "Target"  " ;
esttab m1mid m2mid m3mid m1midfe m2midfe m3midfe  using "${outty}MidPrUNSCpooledInteract.tex",replace unstack ar2 r2 se 
   title("MID involvement and the likelihood of election to UNSC interacted with wealth (Pr(Elected to UNSC) based on model 3 of table~\ref{tab:RotateElectionFE}) \label{tab:MidPrUNSCpooledInteract}" )
   stats( N  fe ttrend,
	labels("Observations" "Country FE" "Time Trends" )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab) order(unsc    FPrUNSCany FPrUNSCanywealth  PrUNSCany PrUNSCanywealth power demaut  logpopVDEM   e_migdppcln) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) 
 drop( yr*  _cons  ) mlabels( $mlab $mlab, nonumbers) eqlabels(none) substitute(\~ $);
 
 
/* count model */
#delimit;
xtpoisson sumMID  $mod, fe i(ccode) ; eststo m1midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCany PrUNSCany; test FPrUNSCany+ PrUNSCany=0;
xtpoisson sumSIDEA  $mod, fe i(ccode) ;eststo m2midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCany PrUNSCany; test FPrUNSCany+ PrUNSCany=0;
xtpoisson sumSIDEB $mod, fe i(ccode) ;eststo m3midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCany PrUNSCany; test FPrUNSCany+ PrUNSCany=0;
/* without FE */
poisson sumMID  $mod, ;eststo m1mid; estadd local fe "N";estadd local ttrend "Y";test FPrUNSCany PrUNSCany; test FPrUNSCany+ PrUNSCany=0;
poisson sumSIDEA  $mod ,  ;
eststo m2mid; estadd local fe "N";estadd local ttrend "Y";
poisson sumSIDEB $mod, ;test FPrUNSCany PrUNSCany; test FPrUNSCany+ PrUNSCany=0;
eststo m3mid; estadd local fe "N";estadd local ttrend "Y";test FPrUNSCany PrUNSCany; test FPrUNSCany+ PrUNSCany=0;

global mlab " "MID" "Initiator" "Target"  " ;
esttab m1mid m2mid m3mid m1midfe m2midfe m3midfe  using "${outty}MidPrUNSCpooledInteractCount.tex",replace unstack ar2 r2 se 
   title("Count model of MID involvement and the likelihood of election to UNSC interacted with wealth (Pr(Elected to UNSC) based on model 3 of table~\ref{tab:RotateElectionFE}) \label{tab:MidPrUNSCpooledInteractCount}" )
   stats( N  fe  ,
	labels("Observations" "Country FE"   )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab) order(unsc    FPrUNSCany FPrUNSCanywealth  PrUNSCany PrUNSCanywealth power demaut  logpopVDEM   e_migdppcln) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) 
 drop( yr*  _cons  ) mlabels( $mlab $mlab, nonumbers) eqlabels(none) substitute(\~ $); 
#delimit;
 poisson sumMID c.P5Ally c.unsc c.PrUNSCpooled##c.e_migdppcln  c.FPrUNSCpooled##c.e_migdppcln   c.power c.demaut  c.logpopVDEM   c.e_migdppcln P5Ally yr* if  single==1 ,vce(cluster ccode);

 #delimit; 
 margins, at(e_migdppcln=(6(.5)11)) dydx(PrUNSCpooled) vsquish ;
  marginsplot, xtitle(log(GDPpc)) ytitle("Impact of Pr(Elected UNSC) on Number of MIDs") title("Average Marginal Effects of Pr(Elected UNSC)") name(marginalPrUNSC,replace) note(Model 1 of Table 6);
 graph export  "${outty}marginsplot.pdf", replace; 
 
  #delimit;
margins  , at(PrUNSCpooled=(0.0 .15)   FPrUNSCpooled=(0.0 .15) 
e_migdppcln=(7 10) ) predict(pr(1)); 

   
 
#delimit;
/***** Poor non-democratic nations *********/ 
regen poorauto=(demaut<.75 & e_migdppcln <8.48),replace; 
regen PrUNSCpoorauto = PrUNSCpooled*poorauto ,replace;
regen FPrUNSCpoorauto = FPrUNSCpooled*poorauto ,replace;
global mod "c.unsc  FPrUNSCpooled FPrUNSCpoorauto  PrUNSCpooled PrUNSCpoorauto  poorauto c.power c.demaut  c.logpopVDEM   c.e_migdppcln yr* if  single==1";


xtlogit MID  $mod, fe i(ccode) ; eststo m1midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCpooled FPrUNSCpoorauto; test PrUNSCpooled PrUNSCpoorauto; 
test FPrUNSCpooled+ PrUNSCpooled=0;
xtlogit SIDEA  $mod, fe i(ccode) ;eststo m2midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCpooled PrUNSCpooled;test FPrUNSCpooled+ PrUNSCpooled=0;
xtlogit SIDEB $mod, fe i(ccode) ;eststo m3midfe; estadd local fe "Y";estadd local ttrend "Y"; test FPrUNSCpooled PrUNSCpooled;test FPrUNSCpooled+ PrUNSCpooled=0;
/* without FE */
logit MID  $mod, ;eststo m1mid; estadd local fe "N";estadd local ttrend "Y";test FPrUNSCpooled PrUNSCpooled;
logit SIDEA  $mod ,  ;
eststo m2mid; estadd local fe "N";estadd local ttrend "Y";
logit SIDEB $mod, ;test FPrUNSCpooled PrUNSCpooled;
eststo m3mid; estadd local fe "N";estadd local ttrend "Y";test FPrUNSCpooled PrUNSCpooled;

global mlab " "MID" "Initiator" "Target"  " ;
esttab m1mid m2mid m3mid m1midfe m2midfe m3midfe  using "${outty}MidPrUNSCpoorAuto.tex",replace unstack ar2 r2 se 
   title("MID involvement and the likelihood of election to UNSC and need for money \label{tab:MidPrUNSCpoorAuto} (Pr(Elected UNSC) based on model 3 of table~\ref{tab:RotateElectionFE})" )
   stats( N  fe  ,
	labels("Observations" "Country FE"   )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab) order(unsc   FPrUNSCpooled FPrUNSCpoorauto  PrUNSCpooled PrUNSCpoorauto poorauto power demaut  logpopVDEM   e_migdppcln) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) 
 drop( yr*  _cons  ) mlabels( $mlab $mlab, nonumbers) eqlabels(none) substitute(\~ $);

 /* END MONADIC ANALYSES */ 



 /*****************************************************************/
 /*****************************************************************/
 /*****************************************************************/
 /********************* Dyadic Analyses ***************************/
 /*****************************************************************/
 /*****************************************************************/
 /*****************************************************************/
#delimit;
use working_UNSCdeter,clear; 

#delimit;
 replace mids=0 if mids==.;
replace primaryAB=0 if primaryAB==.; 
replace primaryHostlevA =0 if primaryHostlevA==. ;
replace primaryHostlevB=0 if primaryHostlevB==. ;
replace PRIMARYABHostlevA=0 if PRIMARYABHostlevA==.; 
replace PRIMARYABHostlevB=0 if PRIMARYABHostlevB==.; 
replace PRIMARYAB=0 if PRIMARYAB==.; 
replace PRIMARYAB=. if obs_num >2; 
replace joiner=0 if joiner==.;/* single yearly observation for Primary */ 

gen forceratio=cincA/(cincA+cincB);
 gen demautA=(polity2A+10)/20;
  gen demautB=(polity2B+10)/20;
  gen double dyad = ccodeA*1000+ccodeB;
   gen logdistance=log(1+kmdist);
   label var unscA "unscA";  label var unscB "unscB";
gen yr=(year-1990)/10;
gen yr2=yr^2;
gen yr3=yr^3;
gen demautAB= demautA*demautB;
gen force2=force^2 ;

sort ccodeA year; rename ccodeA ccode; merge m:1 ccode year using probUNSC;
/*** the probUNSC data were created as part of the Monadic analyses*/

#delimit; 
foreach i of varlist ccode PrUNSCregions PrUNSCpooled PrUNSCbig PrUNSCsmall PrUNSCnoFE FPrUNSCregions FPrUNSCpooled FPrUNSCbig FPrUNSCsmall FPrUNSCnoFE
LPrUNSCbig LPrUNSCsmall LPrUNSCnoFE {; rename `i' `i'A;}; 
 drop _m; rename ccodeB ccode; sort ccode year; 
merge m:1 ccode year using probUNSC;
#delimit; 
foreach i of varlist ccode PrUNSCregions PrUNSCpooled PrUNSCbig PrUNSCsmall PrUNSCnoFE FPrUNSCregions FPrUNSCpooled FPrUNSCbig FPrUNSCsmall FPrUNSCnoFE
LPrUNSCbig LPrUNSCsmall LPrUNSCnoFE {; rename `i' `i'B;}; 
 drop _m; 
sort ccodeA ccodeB year; 

regen PrUNSCpooledwealthA = PrUNSCpooledA*e_migdppclnA ,replace;
regen FPrUNSCpooledwealthA = FPrUNSCpooledA*e_migdppclnA ,replace;

regen poorautoA=(demautA<.75 & e_migdppclnA <8.48),replace; 
regen PrUNSCpoorautoA = PrUNSCpooledA*poorautoA ,replace;
regen FPrUNSCpoorautoA = FPrUNSCpooledA*poorautoA ,replace;

regen PrUNSCpooledwealthB = PrUNSCpooledB*e_migdppclnB ,replace;
regen FPrUNSCpooledwealthB = FPrUNSCpooledB*e_migdppclnB ,replace;

regen poorautoB=(demautB<.75 & e_migdppclnB <8.48),replace; 
regen PrUNSCpoorautoB = PrUNSCpooledB*poorautoB ,replace;
regen FPrUNSCpoorautoB = FPrUNSCpooledB*poorautoB ,replace;


/********* Create Labels for Variables ************/
#delimit;
global lab "unscA " UNSC A"  unscB "UNSC B" 
forceratio "Force Ratio" force2 "Force Ratio \~^2\~" 
demautA "Polity A" demautB "Polity B" demautAB "Polity A * Polity B" AnyAlly "Alliance" IdealPointDistance "Ideal Point Distance" lgdppcA "Log(GDPpc) A" lpopA  "Log(Population) A" ResRentsA "Resource Rents A" lgdppcB "Log(GDPpc) B" lpopB  "Log(Population) B" ResRentsB "Resource Rents B" aidGNIA "Aid/GNI A" aidGNIB "Aid/GNI B" preA "Prior UNSC A" t0A "Year of Election A"  postA "Post UNSC A" 
preB "Prior UNSC B" t0B "Year of Election B"  postB "Post UNSC B"
logpopVDEMA "log(Population) A"  e_migdppclnA "log(GDPpc) A" 
logpopVDEMB "log(Population) B"  e_migdppclnB "log(GDPpc) B" 
PrUNSCpooledA "Pr(Elected UNSC A\~_{}\~)" PrUNSCregionsA "Pr(Elected UNSC A\~_{regions}\~)" 
FPrUNSCpooledA "Pr(Elected UNSC A\~_{t+1}\~)"
FPrUNSCregionsA "Pr(Elected UNSC A\~_{t+1,regions}\~)" PrUNSCpooledB "Pr(Elected UNSC B\~_{}\~)" PrUNSCregionsB "Pr(Elected UNSC B\~_{regions}\~)" 
FPrUNSCpooledB "Pr(Elected UNSC B\~_{t+1}\~)"
FPrUNSCregionsB "Pr(Elected UNSC B\~_{t+1,regions}\~)" PrUNSCpooledA "Pr(Elected UNSC\~_{pooled}\~) A" PrUNSCregionsA "Pr(Elected UNSC\~_{regions}\~) A" 
FPrUNSCpooledA "Pr(Elected UNSC\~_{t+1}\~) A"
FPrUNSCregionsA "Pr(Elected UNSC\~_{t+1,regions}\~) A" 
PrUNSCpooledwealthA "Pr(Elected UNSC)*log(GDPpc) A" FPrUNSCpooledwealthA "Pr(Elected UNSC\~_{t+1}\~)*log(GDPpc) A" 
PrUNSCpoorautoA "Pr(Elected UNSC)*Poor A"  FPrUNSCpoorautoA "Pr(Elected UNSC\~_{t+1}\~)*Poor A"
PrUNSCpooledB "Pr(Elected UNSC \~_{}\~) B" PrUNSCregions "Pr(Elected UNSC\~_{regions}\~) B" 
FPrUNSCpooledB "Pr(Elected UNSC\~_{t+1}\~) B"
FPrUNSCregionsB "Pr(Elected UNSC\~_{t+1,regions}\~) B" 
PrUNSCpooledwealthB "Pr(Elected UNSC)*log(GDPpc) B" FPrUNSCpooledwealthB "Pr(Elected UNSC\~_{t+1}\~)*log(GDPpc) B" 
PrUNSCpoorautoB "Pr(Elected UNSC)*Poor B"  FPrUNSCpoorautoB "Pr(Elected UNSC\~_{t+1}\~)*Poor B" P5AllyA "P5 Ally A" P5AllyB "P5 Ally B"
 ";
label define un -1 "Prior (t-1,t-2)" 0 "Election Year" -2 "No UNSC" 1 "UNSC" 2 "Post UNSC (t+1,t+2)" ,replace; 


gen unA = -2;  replace unA=-1 if tm2A==1| tm1A==1 ; replace unA =0 if t0A==1;
replace unA=1 if t2A==1| t1A==1; replace unA=2 if t3A==1| t4A==1; 
gen unB = -2;  replace unB=-1 if tm2B==1| tm1B==1 ; replace unB =0 if t0B==1;
replace unB=1 if t2B==1| t1B==1; replace unB=2 if t3B==1| t4B==1; 
label value unA un;  label value unB un; 



#delimit;
global SamCriteria0 "year>=1951 & year<=2014 & permA== 0&permB==0 " ;
global SamCriteria2 "year>=1951 & year<=2014 & permA== 0&permB==0 &conttype>0 & conttype<=2"  ;
global SamCriteria3 "year>=1951 & year<=2014 & permA== 0&permB==0 &conttype>0 & conttype<=3" ; 
global SamCriteria3m "year>=1951 & year<=2014 & permA== 0&permB==0 &conttype>0 & conttype<=3 & UNmemberA==1 & UNmemberB==1 "  ;
global SamCriteria4 "year>=1951 & year<=2014 & permA== 0&permB==0 &conttype>0 & conttype<=4" ; 
global SamCriteria5 "year>=1951 & year<=2014 & permA== 0&permB==0 &conttype>0 & conttype<=5" ;

/****************** Summary Statistics of the Basic Finding **************/
#delimit;

tab unA mids if $SamCriteria3, row all; 
tab unA PRIMARYAB if $SamCriteria3, row all;
tab unB mids if $SamCriteria3, row all; 
tab unB PRIMARYAB if $SamCriteria3, row all;

tab mids if $SamCriteria0 &unscA~=. &unscB~=.; 
tab PRIMARYAB  if $SamCriteria0;
#delimit;
capture gen PRIMARYAB100=100*PRIMARYAB; capture gen mids100=100*mids;
table unscA unscB if $SamCriteria3 , statistic( mean mids100)  nformat(%5.2f) ;
table unscA unscB if $SamCriteria3 ,  nformat(%5.0f) ;
table unscA unscB if $SamCriteria3 &obs_num<2, statistic( mean PRIMARYAB100)  nformat(%5.2f) ;
table unscA unscB if $SamCriteria3 &obs_num<2,  nformat(%5.0f) ;
tab unscA PRIMARYAB  if $SamCriteria2 &obs_num<2,all row;

tab joiner unscA if $SamCriteria3;
/* Robustness table */ 


table unscA unscB if $SamCriteria2, statistic( mean mids100)  nformat(%5.2f)  ;
return list;
table unscA unscB if $SamCriteria3m, statistic( mean mids100)  nformat(%5.2f)  ;
return list;
table unscA unscB if $SamCriteria4, statistic( mean mids100)  nformat(%5.2f)  ;
return list;
table unscA unscB if $SamCriteria5, statistic( mean mids100)  nformat(%5.2f)  ;
return list;
table unscA unscB if $SamCriteria0, statistic( mean mids100)  nformat(%5.2f)  ;
return list;
#delimit;
table unscA unscB if $SamCriteria2 & obs_num<2, statistic( mean PRIMARYAB100)  nformat(%5.2f)  ;
return list;
#delimit;
table unscA unscB if $SamCriteria3m & obs_num<2, statistic( mean PRIMARYAB100)  nformat(%5.2f)  ;
return list;
table unscA unscB if $SamCriteria4 & obs_num<2, statistic( mean PRIMARYAB100)  nformat(%5.2f)  ;
return list;
table unscA unscB if $SamCriteria5 & obs_num<2, statistic( mean PRIMARYAB100)  nformat(%5.2f)  ; return list;
table unscA unscB if $SamCriteria0 & obs_num<2, statistic( mean PRIMARYAB100)  nformat(%5.2f)  ;
return list;
return list; 

/****** Summary Table for Pre-UNSC *****/ 
#delimit;
capture gen preA =(tm2A==1| tm1A==1 );
capture gen preA0 =(tm2A==1| tm1A==1 |t0A==1); capture  gen postA =(t3A==1| t4A==1); 
capture gen preB =(tm2B==1| tm1B==1 ); capture gen preB0 =(tm2B==1| tm1B==1 |t0B==1);capture gen postB =(t3B==1| t4B==1); 

table preA preB if $SamCriteria3, statistic( mean mids100)  nformat(%5.2f)  ;
table preA preB if $SamCriteria3,   nformat(%5.0f);
table preB0 preA0 if $SamCriteria3, statistic( mean mids100)  nformat(%5.2f)  ;
table preB0 preA0 if $SamCriteria3,  nformat(%5.0f) ;
table preA0 preB0 if $SamCriteria3 & unscA==0 &unscB==0, statistic( mean mids100)  nformat(%5.2f) ;
 tab preA0 mids if $SamCriteria3, all row;
 
 
 table preB0 preA0 if $SamCriteria3, statistic( mean PRIMARYAB100)  nformat(%5.2f)  ;
table preB0 preA0 if $SamCriteria3,  nformat(%5.0f) ;

/***** Make Figure of probability of disoute by UN status *****/ 
#delimit;
capture frame change default;
capture frame drop makefig;
frame put t1A t2A t3A t4A t0A tm2A tm1A t1B t2B t0B tm2B tm1B t3B t4B PRIMARYAB100 mids100 year permA permB conttype if $SamCriteria3 ,into(makefig ) ;

frame change makefig;

#delimit;
regen tA=-2 if tm2A==1,replace;
regen tA=-1 if tm1A==1,replace;
regen tA=0 if t0A==1,replace;
regen tA=1 if t1A==1,replace;
regen tA=2 if t2A==1,replace;
regen tA=3 if t3A==1,replace;
regen tA=4 if t4A==1,replace;
regen tA=5 if tm2A!=1&tm1A~=1&t0A~=1&t1A~=1&t2A~=1&t3A~=1&t4A~=1,replace;



regen tB=-2 if tm2B==1,replace;
regen tB =-1 if tm1B ==1,replace;
regen tB =0 if t0B ==1,replace;
regen tB =1 if t1B ==1,replace;
regen tB =2 if t2B ==1,replace;
regen tB =3 if t3B ==1,replace;
regen tB =4 if t4B ==1,replace;
regen tB =5 if tm2B !=1&tm1B ~=1&t0B ~=1&t1B ~=1&t2B ~=1&t3B ~=1&t4B ~=1,replace;

label define timeUN  -2 "Election-2" -1 "Election-1" 0 "Election" 1 "UNSC1" 2 "UNSC2"
3 "PostUNSC1" 4 "PostUNSC2" 5 "No UNSC" -3 "No UNSC"; 
save tempUNtime,replace;
collapse PRIMARYAB100 mids100 ,by(tA);
expand 2 if tA==5;
sort tA;
by tA: replace tA=-3 if _n==2; 
 label values tA timeUN;
 sort tA;


twoway (scatter mids100 tA, msymbol(S) mcolor(black) lcolor(black) lwidth(thick) c(l)) (scatter PRIMARYAB100 tA,msymbol(C) mcolor(red) lwidth(thick) lcolor(red) lpattern(dash) c(l)), yscale(range(2 5)) xlabel(-3 (1)5, valuelabel angle(90)) legend(label(1 "MID Involvement on Side A" ) label(2 "Original Dispute Initiator")) title("Likelihood of Dispute Involvement (On Initiator's Side)") ytitle("Dispute Involvement (%)")   xtitle("UNSC Involvement") note(Politically Relevant Dyads) name(basicA,replace); 
 graph export  "${outty}basicA.pdf", replace; 
use tempUNtime,clear; 
collapse PRIMARYAB100 mids100 ,by(tB);
expand 2 if tB==5;
sort tB;
by tB: replace tB=-3 if _n==2; 
label define timeUN  -2 "Election-2" -1 "Election-1" 0 "Election" 1 "UNSC1" 2 "UNSC2"
3 "PostUNSC1" 4 "PostUNSC2" 5 "No UNSC" -3 "No UNSC"; 
 label values tB timeUN;
 sort tB;
#delimit;
twoway (scatter mids100 tB, msymbol(S) mcolor(black) lcolor(black) lwidth(thick) c(l)) (scatter PRIMARYAB100 tB,msymbol(C) mcolor(red) lwidth(thick) lcolor(red) lpattern(dash) c(l)), yscale(range(2 5)) xlabel(-3 (1)5, valuelabel angle(90)) legend(label(1 "MID Involvement on Side B" ) label(2 "Original Dispute Target")) title("Likelihood of Dispute Involvement (On Target's Side)") ytitle("Dispute Involvement (%)")   xtitle("UNSC Involvement") note(Politically Relevant Dyads)   name(basicB,replace); 
graph export  "${outty}basicB.pdf", replace; 

frame change default;
 /************************* Start Analysis Here *********************/
 #delimit ; 
 global mod "unscA unscB force* demaut*  logpopVDEM*  e_migdppcln* P5AllyA P5AllyB AnyAlly yr* if $SamCriteria3 ";
logit  mids  $mod ;
eststo mids1; estadd local FE "N"; estadd local YearS "Y";
count if mids==1 &e(sample)==1; estadd scalar dis r(N);
 #delimit ; 
xtlogit  mids $mod , fe i(dyad);
eststo mids2; estadd local FE "Y"; estadd local YearS "Y";
count if mids==1 &e(sample)==1; estadd scalar dis r(N);
logit  primaryAB $mod  & obs_num<2;
eststo start1; estadd local FE "N"; estadd local YearS "Y";
count if primaryAB==1 &e(sample)==1; estadd scalar dis r(N);
xtlogit  primaryAB $mod  & obs_num<2, fe i(dyad);
eststo start2; estadd local FE "Y"; estadd local YearS "Y";
count if primaryAB==1 &e(sample)==1; estadd scalar dis r(N);
#delimit;
global mlab " "Dispute" "Dispute" "Originator" "Originator" " ;		
esttab mids1 mids2 start1 start2 using "${outty}basicLogit.tex",replace  ar2 r2 se 
   title("Logit Analyses of the Occurrence of Disputes  \label{tab:basicLogit}" )
   stats( N N_g dis FE YearS  ,
	labels("Observations" "Dyads" "MIDs" "Dyad FE" "Year Trends"   )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab) order($o) star(+ 0.10 * 0.05 ** 0.01 *** 0.001)
 drop(   yr yr2 yr3 _cons) mlabels($mlab, nonumbers) eqlabels(none) substitute(\~ $);  
 
 
/**** Probability of Election to UNSC */ 
 #delimit ; 
 global mod "unscA unscB FPrUNSCpooledA PrUNSCpooledA FPrUNSCpooledB PrUNSCpooledB force* demaut*  logpopVDEM*  e_migdppcln*   P5AllyA P5AllyB AnyAlly   yr* if $SamCriteria3 ";
logit  mids  $mod , cluster(dyad);
eststo mids1; estadd local FE "N"; estadd local YearS "Y";
count if mids==1 &e(sample)==1; scalar dis=r(N); estadd local dis dis;
 #delimit ; 
xtlogit  mids $mod , fe i(dyad);
eststo mids2; estadd local FE "Y"; estadd local YearS "Y";
count if mids==1 &e(sample)==1; estadd scalar dis r(N);
test FPrUNSCpooledA PrUNSCpooledA; test FPrUNSCpooledB PrUNSCpooledB;
#delimit;
logit  primaryAB $mod  & obs_num<2;
eststo start1; estadd local FE "N"; estadd local YearS "Y";
count if primaryAB==1 &e(sample)==1; estadd scalar dis r(N);
test FPrUNSCpooledA PrUNSCpooledA; test FPrUNSCpooledB PrUNSCpooledB;
#delimit; 
xtlogit  primaryAB $mod  & obs_num<2, fe i(dyad);
eststo start2; estadd local FE "Y"; estadd local YearS "Y";
count if primaryAB==1 &e(sample)==1; estadd scalar dis r(N);
test FPrUNSCpooledA PrUNSCpooledA; test FPrUNSCpooledB PrUNSCpooledB;
#delimit;
global mlab " "Dispute" "Dispute" "Originator" "Originator" " ;	
esttab mids1 mids2 start1 start2 using "${outty}DyadPrElect.tex",replace  ar2 r2 se 
   title("Occurrence of Disputes, UNSC Membership and the Likelihood of Election \label{tab:DyadPrElect}" )
   stats( N N_g   FE   ,
	labels("Observations" "Dyads"   "Dyad FE"    )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab) order($o) star(+ 0.10 * 0.05 ** 0.01 *** 0.001)
 drop(   yr yr2 yr3 _cons) mlabels($mlab, nonumbers) eqlabels(none) substitute(\~ $); 
 
 /**** Probability of Election to UNSC and Poor Autocracy */ 
 #delimit ; 
 global mod "unscA unscB FPrUNSCpooledA FPrUNSCpoorautoA PrUNSCpooledA PrUNSCpoorautoA poorautoA FPrUNSCpooledB FPrUNSCpoorautoB PrUNSCpooledB  PrUNSCpoorautoB poorautoB force* demaut*  logpopVDEM*  e_migdppcln*  P5AllyA P5AllyB AnyAlly  yr* if $SamCriteria3 ";
logit  mids  $mod , cluster(dyad);
eststo mids1; estadd local FE "N"; estadd local YearS "Y";
count if mids==1 &e(sample)==1; estadd scalar dis r(N);
 #delimit ; 
xtlogit  mids $mod , fe i(dyad);
eststo mids2; estadd local FE "Y"; estadd local YearS "Y";
count if mids==1 &e(sample)==1; estadd scalar dis r(N);
test FPrUNSCpooledA PrUNSCpooledA; test FPrUNSCpooledB PrUNSCpooledB;
#delimit;
logit  primaryAB $mod  & obs_num<2;
eststo start1; estadd local FE "N"; estadd local YearS "Y";
count if primaryAB==1 &e(sample)==1; estadd scalar dis r(N);
test FPrUNSCpooledA PrUNSCpooledA; test FPrUNSCpooledB PrUNSCpooledB;
#delimit; 
xtlogit  primaryAB $mod  & obs_num<2, fe i(dyad);
eststo start2; estadd local FE "Y"; estadd local YearS "Y";
count if primaryAB==1 &e(sample)==1; estadd scalar dis r(N);
test FPrUNSCpooledA PrUNSCpooledA; test FPrUNSCpooledB PrUNSCpooledB;
#delimit;
global mlab " "Dispute" "Dispute" "Originator" "Originator" " ;	
esttab mids1 mids2 start1 start2 using "${outty}DyadPrElectPoor.tex",replace  ar2 r2 se 
   title("Occurrence of Disputes: Poor Non-democracies, UNSC Membership and the Likelihood of Election \label{tab:DyadPrElectPoor}" )
   stats( N N_g   FE   ,
	labels("Observations" "Dyads"   "Dyad FE"    )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab ) order($o) star(+ 0.10 * 0.05 ** 0.01 *** 0.001)
 drop(   yr yr2 yr3 _cons) mlabels($mlab, nonumbers) eqlabels(none) substitute(\~ $); 
 
 
 
/******* Wealth effect **********/
/**** Probability of Election to UNSC and Poor Autocracy */ 
 #delimit ; 
 global mod "unscA unscB FPrUNSCpooledA FPrUNSCpooledwealthA PrUNSCpooledA PrUNSCpooledwealthA  FPrUNSCpooledB FPrUNSCpooledwealthB  PrUNSCpooledB  PrUNSCpooledwealthB force* demaut*  logpopVDEM*  e_migdppcln*  P5AllyA P5AllyB AnyAlly  yr* if $SamCriteria3 ";
logit  mids  $mod , cluster(dyad);
eststo mids1; estadd local FE "N"; estadd local YearS "Y";
count if mids==1 &e(sample)==1; estadd scalar dis r(N);
 #delimit ; 
xtlogit  mids $mod , fe i(dyad);
eststo mids2; estadd local FE "Y"; estadd local YearS "Y";
count if mids==1 &e(sample)==1; estadd scalar dis r(N);
test FPrUNSCpooledA PrUNSCpooledA; test FPrUNSCpooledB PrUNSCpooledB;
#delimit;
logit  primaryAB $mod  & obs_num<2;
eststo start1; estadd local FE "N"; estadd local YearS "Y";
count if primaryAB==1 &e(sample)==1; estadd scalar dis r(N);
test FPrUNSCpooledA PrUNSCpooledA; test FPrUNSCpooledB PrUNSCpooledB;
#delimit; 
xtlogit  primaryAB $mod  & obs_num<2, fe i(dyad);
eststo start2; estadd local FE "Y"; estadd local YearS "Y";
count if primaryAB==1 &e(sample)==1; estadd scalar dis r(N);
test FPrUNSCpooledA PrUNSCpooledA; test FPrUNSCpooledB PrUNSCpooledB;
#delimit;
global mlab " "Dispute" "Dispute" "Originator" "Originator" " ;	
esttab mids1 mids2 start1 start2 using "${outty}DyadPrElectWealth.tex",replace  ar2 r2 se 
   title("Occurrence of Disputes, Wealth, UNSC Membership and the Likelihood of Election \label{tab:DyadPrElectWealth}" )
   stats( N N_g   FE    ,
	labels("Observations" "Dyads"   "Dyad FE"     )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab) order($o) star(+ 0.10 * 0.05 ** 0.01 *** 0.001)
 drop(   yr yr2 yr3 _cons) mlabels($mlab, nonumbers) eqlabels(none) substitute(\~ $);  
 
 

 

/* REPEAT FOR DIFFERENT LEVELS OF CONTIGUITY */
 #delimit ; 
 global mod "unscA unscB FPrUNSCpooledA  PrUNSCpooledA  FPrUNSCpooledB   PrUNSCpooledB   force* demaut*  logpopVDEM*  e_migdppcln*  P5AllyA P5AllyB AnyAlly  yr*  ";
logit  mids   $mod if $SamCriteria0;  eststo c0;estadd local FE "N"; estadd local YearS "Y";count if mids==1 &e(sample)==1; estadd scalar dis r(N);
 logit  mids   $mod if $SamCriteria2; eststo c2;estadd local FE "N"; estadd local YearS "Y";count if mids==1 &e(sample)==1; estadd scalar dis r(N);
  logit  mids  $mod if $SamCriteria3m; eststo c3m;estadd local FE "N"; estadd local YearS "Y";count if mids==1 &e(sample)==1; estadd scalar dis r(N);
 logit  mids   $mod if $SamCriteria4; eststo c4;estadd local FE "N"; estadd local YearS "Y";count if mids==1 &e(sample)==1; estadd scalar dis r(N);
 logit  mids   $mod if $SamCriteria5; eststo c5; estadd local FE "N"; estadd local YearS "Y";count if mids==1 &e(sample)==1; estadd scalar dis r(N);
#delimit;
global mlab "  "\~<= 12\~ miles" "UN members" "\~<=100\~ miles" "\~<= 400\~ miles" "All Dyads" " ;	
esttab  c2 c3m c4 c5 c0 using "${outty}midsContiguity.tex",replace  ar2 r2 se 
   title("Logit Analyses of the Occurrence of Disputes Under Different Contiguity Criteria  \label{tab:midsContiguity}" ) eqlabels(Dispute) 
   stats( N   FE  ,
	labels("Observations"   "Dyad FE"      )fmt(0 0 0 0 %8.2f )) 
 varlabel($lab) order($o) star(+ 0.10 * 0.05 ** 0.01 *** 0.001)
 drop(   yr yr2 yr3 _cons) mlabels($mlab, nonumbers) substitute(\~ $)   ;  
 
 


 

/******** OUTCOME AND HOSTILITY Levels ***********/ 
#delimit ; 
tab outcome;
gen ProAoutcome=0 if outcome~="";;
replace ProAoutcome=-1 if (outcome=="Win B" |outcome=="Yield A");
replace ProAoutcome=1 if (outcome=="Win A" |outcome=="Yield B");
oprobit  ProAoutcome    $mod if $SamCriteria3;
/*** hostility ***/
#delimit;
regen hostDiff=primaryHostlevA-primaryHostlevB,replace; 
oprobit  hostDiff   $mod if $SamCriteria3  & primaryHostlevA~=0;
oprobit  primaryHostlevA    $mod if $SamCriteria3  & primaryHostlevA~=0
; eststo opA; estadd local FE "N"; estadd local YearS "Y";
count if mids==1 &e(sample)==1; estadd scalar dis r(N);

oprobit  primaryHostlevB   $mod if $SamCriteria3 &  primaryHostlevB~=0
; eststo opB; estadd local FE "N"; estadd local YearS "Y";
count if mids==1 &e(sample)==1; estadd scalar dis r(N);
xtreg  primaryHostlevA  $mod if $SamCriteria3 & primaryHostlevB~=0, fe i(dyad);
eststo xtreghostA; estadd local FE "Y"; estadd local YearS "Y";
count if mids==1 &e(sample)==1; estadd scalar dis r(N);
xtreg  primaryHostlevB $mod if $SamCriteria3  & primaryHostlevB~=0, fe i(dyad);
eststo xtreghostB;estadd local FE "Y"; estadd local YearS "Y";
count if mids==1 &e(sample)==1; estadd scalar dis r(N);
#delimit;
esttab opA opB xtreghostA xtreghostB using "${outty}Hostility.tex",replace  ar2 r2 se 
   title("Hostility Level in Disuptes  Occurrence of Disputes  \label{tab:Hostility}" )
   stats( N N_g dis FE YearS  ,
	labels("Observations" "Dyads" "MIDs" "Dyad FE" "Year Trends"   )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab) order($o) star(+ 0.10 * 0.05 ** 0.01 *** 0.001)
 drop(   yr yr2 yr3 _cons) mlabels($mlab, nonumbers) eqlabels(none) substitute(\~ $);   



xtreg  PRIMARYABHostlevB  PRIMARYABHostlevA   unsc* force* demaut* yr* if $SamCriteria3 & obs_n <2 & PRIMARYABHostlevA~=0, fe i(dyad);

xtreg  PRIMARYABHostlevA  PRIMARYABHostlevB   unsc* force* demaut* yr* if $SamCriteria3 & obs_n <2 & PRIMARYABHostlevA~=0, fe i(dyad);
/*** seemingly unrelated regression*/ 
#delimit;
sureg (hostA: primaryHostlevA    unsc* force* demaut* yr* if $SamCriteria3 &mids==1)
(hostB: primaryHostlevB    unsc* force* demaut* yr* if $SamCriteria3 & mids==1) if $SamCriteria3 &mids==1, i corr;

#delimit;
sureg (hostA: hihosta    unsc* force* demaut* yr* if $SamCriteria3 &mids==1)
(hostB:hihostb    unsc* force* demaut* yr* if $SamCriteria3 & mids==1) if $SamCriteria3 &mids==1, i corr;
eststo surr1;
#delimit;
esttab surr1 using "${outty}sureg.tex",replace unstack ar2 r2 se 
   title("Logit Analyses of the Occurrence of Disputes  \label{tab:sureg}" )
   stats( N N_g dis FE YearS  ,
	labels("Observations" "Dyads" "MIDs" "Dyad FE" "Year Trends"   )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab) order($o) star(+ 0.10 * 0.05 ** 0.01 *** 0.001)
 drop(   yr yr2 yr3 _cons) mlabels($mlab, nonumbers) eqlabels(none) substitute(\~ $);  
#delimit ;
/* Escalation to violence and war */ 
gen violence = (hihosta>=4 & hihostb>=4) ;
gen war5 = (hihosta>=5 & hihostb>=5) ;
table unscA unscB if PRIMARYAB==1 & permA==0&permB==0 , statistic( mean violence)  nformat(%5.2f) ;

 #delimit;
tab unscA violence   if mids==1 & permA==0&permB==0 ,all row;
tab unscB violence  if mids==1 & permA==0&permB==0 ,all row;
tab unscA violence  if PRIMARYAB==1 & permA==0&permB==0 ,all row;
tab unscB violence  if PRIMARYAB==1 & permA==0&permB==0 ,all row;


tab war5 unscA  if mids==1 & permA==0&permB==0 ,all col;
tab war5 unscB  if mids==1 & permA==0&permB==0 ,all col;
tab war5 unscA  if PRIMARYAB==1 & permA==0&permB==0 ,all col;
tab war5 unscB  if PRIMARYAB==1 & permA==0&permB==0 ,all col;


#delimit ; 
logit  violence  $mod  if  mids==1 &permA==0&permB==0 ;
eststo v1; estadd local YearS "Y"; estadd local fe "N";
logit  violence  $mod   if  PRIMARYAB==1 &permA==0&permB==0;
eststo v2;estadd local YearS "Y";estadd local fe "N";
xtlogit  violence  $mod  if  mids==1 &permA==0&permB==0 , fe i(dyad);
eststo v3; estadd local YearS "Y";estadd local fe "Y";
xtlogit  violence  $mod  if  PRIMARYAB==1 &permA==0&permB==0, fe i(dyad);
eststo v4;estadd local YearS "Y";estadd local fe "Y";
#delimit;
global mlab " "Dispute"  "Originator" " ;
esttab v1   v2 v3 v4  using "${outty}violence.tex",replace unstack ar2 r2 se 
   title("Escalation of Disputes to Violence (defined as a hostility level of 4 or 5) \label{tab:violence}" )
   stats( N fe YearS  ,
	labels("Observations" "Dyad FE" "Year Trends"   )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab) order($o) star(+ 0.10 * 0.05 ** 0.01 *** 0.001)
 drop( yr*   _cons) mlabels($mlab $mlab, nonumbers) eqlabels(none) substitute(\~ $); 
 

 
/***************************************************/
/*********Huth and Allee Data on concessions *******/
/******** Negotitations in teritotial disputes *****/
/***************************************************/
/***************************************************/
/**** Impact of UNSC on dispute behavior */
/* Use Huth and Allee data 
@data{DVN/XEGO5E_2009,
author = {Paul Huth and Todd Allee},
publisher = {Harvard Dataverse},
title = {{The Democratic Peace and Territorial Conflict in the Twentieth Century}},
UNF = {UNF:3:O0SiGIW9tqM+bF+UEGn7uA==},
year = {2009},
version = {V1},
doi = {10.7910/DVN/XEGO5E},
url = {https://doi.org/10.7910/DVN/XEGO5E}
} */ 
 



#delimit; 
use "huth_allee_statusquodataREADY.dta",clear; 


regen DiffNego=dvneg1- dvneg2 if dvsq1==1, replace; 
label var DiffNego "Difference in concessions btwn A & B in negotiations";
regen DiffHost=dvesc1- dvesc2 if dvsq1==2, replace; 
label var DiffHost "Difference in hostilitie btwn A & B in disputes";
/* dvsq1=0 nothing; 1 = start nego; 2 =start MID*/
des dvsq1; 
tab unA dvsq1 , all row;
des dvneg1;
tab unA dvneg1 if dvsq1==1  , all row;
des dvesc1; 
tab unA dvesc1 , all row;
des dvneg2; 
tab unA dvneg2 if dvsq1==1, all row;
des dvesc2;
tab unA dvesc2 , all row;

#delimit;
tab unA DiffNego , all row; 
tab unB DiffNego , all row; 
tab unA DiffHost , all row; 
tab unB DiffHost , all row; 


tab unB dvsq1 , all row;
tab unB dvneg1 , all row;
tab unB dvesc1 , all row;

tab unB dvneg2 , all row;
tab unB dvesc2 , all row;

regen startneg=(dvsq1==1),replace; 
regen startneg=. if dvsq1==2, replace; 

/**** Basic model specifications ******/
global mod "c.unsc  c.FPrUNSCpooled c.FPrUNSCpooledwealth  c.PrUNSCpooled c.PrUNSCpooledwealth   c.power c.demaut  "; global mod "unscA unscB FPrUNSCpooledA PrUNSCpooledA FPrUNSCpooledB PrUNSCpooledB force* demaut*  logpopVDEM*  e_migdppcln* ";
global poor " unscA unscB FPrUNSCpooledA FPrUNSCpoorautoA PrUNSCpooledA PrUNSCpoorautoA poorautoA  FPrUNSCpooledB FPrUNSCpoorautoB PrUNSCpooledB PrUNSCpoorautoB poorautoB force* demaut*  logpopVDEM*  e_migdppcln* ";
#delimit;
global xvar "preA t0A unscA postA  preB t0B unscB postB c.demautA demautB demautAB  force* ";
global xvar2 " preA0 unscA postA  preB0 unscB  postB c.demautA demautB demautAB  force* ";
global xvarshort " unscA  unscB   c.demautA demautB demautAB  force* ";
global xvarshort2 " unscA  unscB   c.demautA demautB demautAB  force* logpopVDEM*  e_migdppcln*";
/* cannot readily predict the start of negotiations */ 
logit startneg $mod; 
 
 #delimit;
reg DiffNego $xvar if dvsq1==1; 
eststo v1; estadd local meth "OLS"; estadd local fe "N"; ;
xtreg DiffNego $xvar if dvsq1==1, fe i(dyad);
eststo v2; estadd local meth "OLS w. FE"; estadd local fe "Y"; 
ologit DiffNego $xvar if dvsq1==1;
eststo v3; estadd local meth "Ordered-logit"; estadd local fe "N"; 
global xvar "preA t0A unscA postA  preB t0B unscB postB c.demautA demautB demautAB  force* logpopVDEM*  e_migdppcln*";
reg DiffNego $xvar if dvsq1==1; 
eststo v4;  estadd local meth "OLS";estadd local fe "N"; 
xtreg DiffNego $xvar if dvsq1==1, fe i(dyad);
eststo v5; estadd local meth "OLS w. FE"; estadd local fe "Y"; 
ologit DiffNego $xvar if dvsq1==1;
eststo v6; estadd local meth "Ordered-logit"; estadd local fe "N"; 

global mlab " "Concessions"  "Concessions"  "Concessions"" ;
esttab v1   v2 v3 v4 v5 v6 using "${outty}concessions.tex",replace unstack ar2 r2 se 
   title("Difference between Challenger and Target Concessions during Negotiations of Terroritoral Disputes  \label{tab:concessions}" )
   stats( N   meth fe,
	labels("Observations"  "Method" "FE"  )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab) order($o) star(+ 0.10 * 0.05 ** 0.01 *** 0.001)
 drop( cut*   _cons) mlabels($mlab $mlab, nonumbers) eqlabels(none) substitute(\~ $); 
 
#delimit;
reg DiffNego $xvarshort if dvsq1==1; 
eststo v1;  estadd local fe "N"; estadd local meth "OLS";
xtreg DiffNego $xvarshort if dvsq1==1, fe i(dyad);
eststo v2;  estadd local fe "Y"; estadd local meth "OLS w. FE";
ologit DiffNego $xvarshort if dvsq1==1;
eststo v3;  estadd local fe "N"; estadd local meth "Ordered-logit";

reg DiffNego $xvarshort2 if dvsq1==1; 
eststo v4;  estadd local fe "N"; estadd local meth "OLS";
xtreg DiffNego $xvarshort2 if dvsq1==1, fe i(dyad);
eststo v5;  estadd local fe "Y"; estadd local meth "OLS w. FE";
ologit DiffNego $xvarshort2 if dvsq1==1;
eststo v6;  estadd local fe "N"; estadd local meth "Ordered-logit";

global mlab " "Concessions"  "Concessions"  "Concessions"" ;
esttab v1   v2 v3 v4 v5 v6 using "${outty}concessionsshort.tex",replace unstack ar2 r2 se 
   title("Difference between Challenger and Target Concessions during Negotiations of Terroritoral Disputes  \label{tab:concessionsshort}" )
   stats( N fe meth ,
	labels("Observations" "Dyad FE" "Method"   )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab) order($o) star(+ 0.10 * 0.05 ** 0.01 *** 0.001)
 drop( cut*   _cons) mlabels($mlab $mlab, nonumbers) eqlabels(none) substitute(\~ $);  
 
 
 /****** Concessions and Probability of Election to UNSC */
 
#delimit;
reg DiffNego $mod if dvsq1==1; 
test FPrUNSCpooledA PrUNSCpooledA;
test  FPrUNSCpooledB PrUNSCpooledB;
test FPrUNSCpooledA +PrUNSCpooledA=0;
test  FPrUNSCpooledB +PrUNSCpooledB=0;
eststo v1;  estadd local fe "N"; estadd local meth "OLS";
xtreg DiffNego $mod if dvsq1==1, fe i(dyad);
test FPrUNSCpooledA PrUNSCpooledA;
test  FPrUNSCpooledB PrUNSCpooledB;
test FPrUNSCpooledA +PrUNSCpooledA=0;
test  FPrUNSCpooledB +PrUNSCpooledB=0;
eststo v2;  estadd local fe "Y"; estadd local meth "OLS w. FE";
ologit DiffNego $mod if dvsq1==1;
test FPrUNSCpooledA PrUNSCpooledA;
test  FPrUNSCpooledB PrUNSCpooledB;
test FPrUNSCpooledA +PrUNSCpooledA=0;
test  FPrUNSCpooledB +PrUNSCpooledB=0;
eststo v3;  estadd local fe "N"; estadd local meth "Ordered-logit";


global mlab " "Concessions"  "Concessions"  "Concessions"" ;
esttab v1   v2 v3  using "${outty}concessionsProb.tex",replace unstack ar2 r2 se 
   title("Difference between Challenger and Target Concessions during Negotiations of Terroritoral Disputes: Afect of UNSC and Likelihood of Election \label{tab:concessionsProb}" )
   stats( N fe meth ,
	labels("Observations" "Dyad FE" "Method"   )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab) order($o) star(+ 0.10 * 0.05 ** 0.01 *** 0.001)
 drop( cut*   _cons) mlabels($mlab $mlab, nonumbers) eqlabels(none) substitute(\~ $); 
 
 /********* poor auocracies ********/
 regen poorautoA=(demautA<.75 & e_migdppclnA <8.48),replace; 
regen PrUNSCpoorautoA = PrUNSCpooledA*poorautoA ,replace;
regen FPrUNSCpoorautoA = FPrUNSCpooledA*poorautoA ,replace;
 regen poorautoB=(demautB<.75 & e_migdppclnB <8.48),replace; 
regen PrUNSCpoorautoB = PrUNSCpooledB*poorautoB ,replace;
regen FPrUNSCpoorautoB = FPrUNSCpooledB*poorautoB ,replace;
#delimit;
reg DiffNego $poor if dvsq1==1; 
test FPrUNSCpooledA PrUNSCpooledA;
test  FPrUNSCpooledB PrUNSCpooledB;
test FPrUNSCpooledA +PrUNSCpooledA=0;
test  FPrUNSCpooledB +PrUNSCpooledB=0;
test FPrUNSCpooledA PrUNSCpooledA PrUNSCpoorautoA FPrUNSCpoorautoA;
test FPrUNSCpooledA +PrUNSCpooledA+PrUNSCpoorautoA+ FPrUNSCpoorautoA=0;
test FPrUNSCpooledB PrUNSCpooledB PrUNSCpoorautoB FPrUNSCpoorautoB;
test FPrUNSCpooledB +PrUNSCpooledB+PrUNSCpoorautoB+ FPrUNSCpoorautoB=0;




eststo v1;  estadd local fe "N"; estadd local meth "OLS";
xtreg DiffNego $poor if dvsq1==1, fe i(dyad);
eststo v2;  estadd local fe "Y"; estadd local meth "OLS w. FE";
ologit DiffNego $poor if dvsq1==1;
eststo v3;  estadd local fe "N"; estadd local meth "Ordered-logit";


global mlab " "Concessions"  "Concessions"  "Concessions"" ;
esttab v1   v2 v3  using "${outty}concessionsPoor.tex",replace unstack ar2 r2 se 
   title("Difference between Challenger and Target Concessions during Negotiations of Terroritoral Disputes: Poor Autocracies  \label{tab:concessionsPoor}" )
   stats( N fe meth ,
	labels("Observations" "Dyad FE" "Method"   )fmt(0 0 0 0 0 %8.2f )) 
 varlabel($lab poorautoA "Poor, Autocrat A" poorautoB "Poor, Autocrat B") order($o) star(+ 0.10 * 0.05 ** 0.01 *** 0.001)
 drop( cut*   _cons) mlabels($mlab $mlab, nonumbers) eqlabels(none) substitute(\~ $); 
 

/*** Length of Negotiation -- Trouble is that both sides control time **/ 
reg length $xvar if dvsq1==1;
xtreg length  $xvar if dvsq1==1, fe i(dyad);










 /*#delimit; 
  erase temp_dis.dta; erase tempUNB.dta;  erase polity_temp.dta ;
 erase cinc_temp.dta; erase ally_temp.dta;  erase midb_temp.dta;erase tempDYAD.dta; erase tempDIS.dta;*/
 

 